package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.pojo.Commodity;

import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.CommodityMapper;
import com.groupseven.happycampus.mapper.OwnCommodityMapper;
import com.groupseven.happycampus.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service(value = "commodityService")
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private OwnCommodityMapper ownCommodityMapper;


    @Override
    public LayuiPageVo<Commodity> commodityListPageData(Map<String, Object> params) {

        //设置分页查询的内容
        int limit = Integer.parseInt(params.get("limit").toString());
        int page = Integer.parseInt(params.get("page").toString());
        PageHelper.startPage(page,limit);


        //查询结果
        List<Commodity> commodityList =  commodityMapper.selectCommodityList(params);

        PageInfo<Commodity> commodityPageInfo = new PageInfo<>(commodityList);

        LayuiPageVo<Commodity> commodityLayuiPageVo = new LayuiPageVo<>();
        commodityLayuiPageVo.setCode(0);
        commodityLayuiPageVo.setMsg("商品列表分页查询");
        commodityLayuiPageVo.setCount(commodityPageInfo.getTotal());
        commodityLayuiPageVo.setData(commodityPageInfo.getList());

        return commodityLayuiPageVo;


    }

    @Override
    public int addCommodity(Commodity commodity) {
       return commodityMapper.addCommodity(commodity);
    }

    @Override
    public int deleteCommodity(Integer commodityId) {
        return commodityMapper.deleteCommodity(commodityId);
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        return commodityMapper.updateCommodity(commodity);
    }


    /**
     * 交易类中的购买操作方法
     * @param commodity 购买的商品
     * @return 返回交易结果
     */


    //明儿有时间了看看事务怎么在这用！！！

    /**
     *
     * @param commodity 购买的商品 Map类型
     * @return
     */
    @Override
    public R purchasedCommodity(Commodity commodity) {
        Integer commodityId = commodity.getCommodityId();
        int i = commodityMapper.deleteCommodity(commodityId);
        if (i != 1){
            System.out.println("库销售失败");
            return R.error();
        }

        // 将 status 状态设置为 false
        commodity.setStatus(false);
        int i1 = ownCommodityMapper.purchaseCommodity(commodity);
        if (i1 != 1){
            System.out.println("购入商品失败");
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R purchasedCommodityByIds(String ids) {
        String[] id = ids.split(","); //[1,2]
        List<Commodity> commodityList = commodityMapper.selectCommodityIdById(id);
        int i = 0;
        int j = 0;

        try{
            for(Commodity commodity : commodityList){
                //把上架状态设置为 true
                commodity.setStatus(true);
//                i += ownCommodityMapper.purchaseCommodity(commodity);
                i += ownCommodityMapper.putOnTheShelfCommodity(commodity);

                j += ownCommodityMapper.deleteOwnCommodity(commodity.getCommodityId());


            }
            if(i != commodityList.size() && j != commodityList.size() ){
                System.out.println("购入商品失败");
                return R.error();
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return R.ok();
    }
}
